<?php
namespace app\api\controller;

use app\lib\Show;
use function Composer\Autoload\includeFile;
use think\Exception;
use think\facade\Log;
use think\response\Json;

class Product extends Base {

    public function search() : Json {
        $params = $this->request->param([
            "category_id",
            "page",
            "page_size"], 0, "intval");
        $keyword = $this->request->param(["keyword"], "", "trim");
        $data = array_merge($params, $keyword);

        if ($data["category_id"] <= 0 && empty($keyword)) {
            return Show::fail("参数获取失败");
        }

        try{
            $result = (new \app\api\business\Product())->search($data);
            $result["list"] = $result["data"];
            $result["count"] = $result["total"];
            unset($result["data"], $result["total"]);
            return Show::success("ok", $result);
        }catch(Exception $e){
            Log::error($e);
            return Show::fail("获取数据失败");
        }

    }

    public function detail($id = 0) : Json {

        if($id <= 0) {
            return Show::fail("获取参数失败");
        }

        try{
            $result = (new \app\api\business\Product())->detail($id);
            return Show::success("ok", $result);
        }catch(Exception $e){
            Log::error($e);
            return Show::fail("获取数据失败");
        }

    }
}